package com.user.service;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;

import com.bean.Admin;
import com.bean.Pet;
import com.bean.Type;
import com.bean.User;
import com.user.dao.UserDao;
@Service("userServiceImp")
public class UserServiceImp implements UserService {
	@Autowired UserDao userDao;
	
	public String check(User user,HttpSession session,Model model) {
		// TODO Auto-generated method stub
		User loginuser=userDao.Check(user);
		if(loginuser==null) {
			model.addAttribute("msg","用户名错误");
			return "login";
		}else {
			session.setAttribute("user", loginuser);
			return "user/index";
		}
	}
	
	public String insertUser(User user, BindingResult result,Model model) {
		// TODO Auto-generated method stub
		if(!user.getUserpass().equals(user.getUserpass1())) {
			return "user/regerror";
		}
		if(result.hasErrors()) {
			return "user/reguser";
		}
		int iresult=userDao.insertUser(user);
		if(iresult<0) {
			return "user/reguser";
		}else {
			model.addAttribute("mess","账号已注册，请您登录");
			return "forward:login";
		}
	}
	
	public String adminLogin(User user,Model model,HttpSession session) {
		// TODO Auto-generated method stub
		Admin admin=new Admin();
		admin.setLoginname(user.getUsername());
		admin.setLoginpass(user.getUserpass());
		Admin a=userDao.selectAdmin(admin);
		if(a==null) {
			model.addAttribute("msg","管理员账号不存在");
			return "login";
		}else {
			session.setAttribute("admin", admin);
			return "admin/houtai";
		}
	}
	
	public String updateInfo1(HttpSession session, User user,Model model) {
		// TODO Auto-generated method stub
			int result=userDao.updateInfo1(user);
			if(result>0) {
				session.invalidate();
				model.addAttribute("mess","修改成功");
				return "forward:MainPet";
			}else {
				return "user/fail";
			}
		
		
	}
	
	public String UserSelectPetByPname(String pname, Model model) {
		// TODO Auto-generated method stub
		List<Pet>list=userDao.UserSelectPetByPname(pname);
		model.addAttribute("list",list);
		model.addAttribute("pname",pname);
		List<Type>listtype=userDao.selectPettype();
		model.addAttribute("listtype",listtype);
		return "user/ShowPet";
	}
	
	public String selectbyPettype(int tid, Model model) {
		// TODO Auto-generated method stub
		List<Map<String,Object>>list=userDao.selectPetByTid(tid);
		model.addAttribute("list",list);
		List<Type>listtype=userDao.selectPettype();
		model.addAttribute("listtype",listtype);
		return "user/ShowPet";
	}
	
	public String UserSelectPet(Model model) {
		// TODO Auto-generated method stub
		List<Map<String,Object>>list=userDao.getPet();
		model.addAttribute("list",list);
		List<Type>listtype=userDao.selectPettype();
		model.addAttribute("listtype",listtype);
		return "user/ShowPet";
	}
	
	public String UserSelectPetByType(Model model) {
		// TODO Auto-generated method stub
		List<Map<String,Object>>list=userDao.getPet();
		model.addAttribute("list",list);
		List<Type>listtype=userDao.selectPettype();
		model.addAttribute("listtype",listtype);
		return "user/Petshowbytype";
	}
	
	public String selectPetByID(int pid, Model model) {
		// TODO Auto-generated method stub
		Pet Pet=userDao.selectPetByID(pid);
		model.addAttribute("Pet",Pet);
		return "user/Petdetailshow";
	}
	
	public String MainPet(Model model) {
		// TODO Auto-generated method stub
		List<Map<String,Object>>list=userDao.getPet();
		model.addAttribute("list",list);
		return "user/MainPet";
	}
	
}
